package com.aios.service.impl;

import java.util.Calendar;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.aios.dao.SystemUserMapper;
import com.aios.dao.SystemUserPasswordMapper;
import com.aios.entity.SystemUser;
import com.aios.entity.SystemUserPassword;
import com.aios.service.PasswordChangeService;

@Service("passwordChangeService")
public class PasswordChangeServiceImpl extends AbstractListService implements PasswordChangeService {
	@Autowired
	private SystemUserMapper systemUserMapper;

	@Autowired
	private SystemUserPasswordMapper systemUserPasswordMapper;
	
	@Override
	public SystemUser currentUser(String email) {
		
		List<SystemUser> list = systemUserMapper.findByEmailAddress(email);

		if (list == null) {
			// error
		} else if (list.size() == 0) {
			// user not found
		} else if (list.size() > 1) {
			// too many users
		} else {
			Calendar calendar = Calendar.getInstance();
			list.get(0).setLastLogin(calendar.getTime());
			return list.get(0);
		}

		return null;
	}

	@Override
	public int saveNewPasswordAndNewStatus(SystemUser systemUser, Integer userId) { 
		saveEditSettings(systemUser, userId);
		return systemUserMapper.updateByPrimaryKeySelective(systemUser);
		
	}


	@Override
	public int savePasswordHistory(SystemUser systemUser, Integer id) {
		int retVal;
		Calendar calendar = Calendar.getInstance();
		
		SystemUserPassword record = new SystemUserPassword();
		record.setSystemUserId(	systemUserMapper.findByEmailAddress(systemUser.getEmail()).get(0).getId());
		record.setPasswordChangeDate(calendar.getTime());
		
		saveAddSettings(record, id);
		retVal=systemUserPasswordMapper.insertWithoutId(record);
		
		return retVal;
	}


}
